<?php
namespace Users\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Expression;
class SentNotificationsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addsentNotified($userId,$offer_id)
    {
		$data = array(
			'user_id'		       => 	$userId, 	
			'offer_id'   	       => 	$offer_id,
			'notification_status'  => 	1,
			'mailed_status'   	   => 	1,
			'count_status'   	   => 	1,
			'added_date' 	       =>   date('Y-m-d H:i:s'),		
			'updated_date' 	       =>   date('Y-m-d H:i:s'),	
		);
		$this->tableGateway->insert($data);
		return $this->tableGateway->lastInsertValue;
    }
	public function getallusersemails(){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_users', 'clf_sent_offer_notifications.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_offers', 'clf_sent_offer_notifications.offer_id=clf_offers.offer_id',array('*'),'left');
		$select->where('clf_sent_offer_notifications.mailed_status="1"');
		$resultSet = $this->tableGateway->selectWith($select);	
        return $resultSet;		
	}
	public function updatemailstatus($ids){
		$data = array(
			'mailed_status' 	 => 0,
			'updated_date' 	     =>   date('Y-m-d H:i:s'),
		 );	
		$result=$this->tableGateway->update($data, array('sent_offer_notification_id' => $ids));
		return $result;
	}
	public function getusernotifications($userId){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_users', 'clf_sent_offer_notifications.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_offers', 'clf_sent_offer_notifications.offer_id=clf_offers.offer_id',array('*'),'left');
		$select->where('clf_sent_offer_notifications.count_status="0"');
		//$select->where('clf_sent_offer_notifications.mailed_status="0"');
		$select->where('clf_offers.delete_offer="0"');
		$select->where('clf_sent_offer_notifications.user_id="'.$userId.'"');
		$select->order('clf_offers.offer_id DESC'); 
		$resultSet = $this->tableGateway->selectWith($select);
        return $resultSet;	
	}
	public function getcountusernotifications($userId){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_users', 'clf_sent_offer_notifications.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_offers', 'clf_sent_offer_notifications.offer_id=clf_offers.offer_id',array('*'),'left');
		$select->where('clf_sent_offer_notifications.count_status="1"');
		//$select->where('clf_sent_offer_notifications.mailed_status="0"');
		$select->where('clf_offers.delete_offer="0"');
		$select->where('clf_sent_offer_notifications.user_id="'.$userId.'"');
		$result = $this->tableGateway->selectWith($select);
        return $result;	
	}
	public function updatecountstatus($user_id){
		$data = array(
			'count_status' 	 => 0,
			'updated_date' 	     =>   date('Y-m-d H:i:s'),
		 );	
		$result=$this->tableGateway->update($data, array('user_id' => $user_id,'count_status'=>'1'));
		return $result;
	}
	public function changeStatus($offerIds,$value)
	{
		$data = array(
			'notification_status'       =>$value,
		);
		$updateStatus=$this->tableGateway->update($data,array('(offer_id IN ('.$offerIds.'))'));
		return 	$updateStatus;			
	}
	public function getUserOffers($userId,$curr_date){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_users', 'clf_sent_offer_notifications.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_offers', 'clf_sent_offer_notifications.offer_id=clf_offers.offer_id',array('*'),'left');
		$select->where('clf_offers.starting_date<="'.$curr_date.'"');
		$select->where('clf_offers.ending_date>="'.$curr_date.'"');
		$select->where('clf_offers.status="1"');
		$select->where('clf_offers.delete_offer="0"');
		if($userId=='admin_id'){
			$select->where('clf_sent_offer_notifications.user_id="'.$userId.'"');
		}
		$resultSet = $this->tableGateway->selectWith($select);
        return $resultSet;	
	}
}